/**
 * login 登录页数据
 */

import { loginAjax, regCapAjax, registerAjax } from '@/api/login'
export default {
  namespaced: true,
  state: {
    token: '',
    userInfo: {},
    msg: ''
  },
  mutations: {
    commitLoginData (state, result) {
      state.userInfo = JSON.stringify(result.userInfo)
      state.token = result.token
      window.localStorage.setItem('token', state.token)
      window.localStorage.setItem('userInfo', state.userInfo)
    },
    commitRegisterData (state, result) {
      state.userInfo = JSON.stringify(result.data.userInfo)
      state.token = result.data.token
      state.msg = result.errmsg
      window.localStorage.setItem('userInfo', state.userInfo)
      window.localStorage.setItem('token', state.token)
    }
  },
  actions: {
    goLogin ({ commit }, { username, password }) {
      loginAjax({ username, password }).then(res => {
        commit('commitLoginData', res.data)
      })
    },
    //  手机号注册，获取验证码
    getPhoneCode ({ commit }, option) {
      regCapAjax(option).then(res => {
        console.log(res)
      })
    },
    // 注册账号
    goRegister ({ commit }, options) {
      registerAjax(options).then(res => {
        console.log(res)
        commit('commitRegisterData', res)
      }).catch(err => {
        console.log(err)
      })
    }

  },
  getters: {}
}
